package com.farecrawler.app.main;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.farecrawler.app.exception.FareCrawlException;
import com.farecrawler.app.job.TigerAirJob;
import com.farecrawler.app.utils.CountriesUtils;

/**
 * Farecrawler Entry point
 * @author Zing
 */
@Component("fareCrawlMain")
public class FareCrawlMain {
	
	/*
	 * Jobs
	 */
	@Resource(name="tigerAirJob")
	private TigerAirJob tigerJob;
	
	public void start() {
		
		List<Calendar> departDateList = calculateDateRange();
		
		try {
			runTigerAirJob(departDateList, CountriesUtils.SINGAPORE, CountriesUtils.BANGKOK_INTL);
			
		}catch(FareCrawlException e) {
			e.printStackTrace();
		}
		
	}
	
	private List<Calendar> calculateDateRange(){
		
		Calendar cal = Calendar.getInstance();
		int dayOfTheMonth = cal.get(Calendar.DAY_OF_MONTH);
		List<Calendar> result = new ArrayList<Calendar>();
		
		int amount = 15;
		for(int i=0; i<=amount; i++) {
			Calendar tmp = Calendar.getInstance();
			tmp.set(Calendar.DATE, (dayOfTheMonth+i));
			result.add(tmp);
		}
		return result;
	}
	
	private void runTigerAirJob(List<Calendar> dates, String origin, String destination) throws FareCrawlException{
		tigerJob.startJob(dates, origin, destination);	
	}

	/*
	 * Getters Setters
	 */
	public TigerAirJob getTigerJob() {
		return tigerJob;
	}
	public void setTigerJob(TigerAirJob tigerJob) {
		this.tigerJob = tigerJob;
	}
	
	

}
